package main

import (
	// "github.com/codegangsta/cli"
	"github.com/codeskyblue/go-sh"

	"bufio"
	// "bytes"
	"fmt"
	"log"
	"os"
	// "os/exec"
	"strconv"
	"strings"
)

func main() {

	log.Println("start ... ")
	fmt.Print("> ")

	var fm Fm
	fm.IsLogin = false

	reader := bufio.NewReader(os.Stdin)

	for {
		input, _, err := reader.ReadLine()
		if err != nil {
			fmt.Println(err.Error())
			break
		}

		inln := strings.Split(string(input), " ")
		inlnLength := len(inln)

		switch inln[0] {
		case "bye":
			os.Exit(0)
		case "login", "who":
			if !fm.IsLogin {

				var account, pwd string

				if inlnLength > 2 {
					account = inln[1]
					pwd = inln[2]
				} else if inlnLength > 1 {
					account = inln[1]
					fmt.Print("PassWord:")
					input, _, _ = reader.ReadLine()
					pwd = string(input)
				} else {
					fmt.Print("Email:")
					input, _, _ = reader.ReadLine()
					account = string(input)
					fmt.Print("PassWord:")
					input, _, _ = reader.ReadLine()
					pwd = string(input)
				}

				fm.OauthLogin(account, pwd)
				if !fm.IsLogin {
					fmt.Println("login fail!")
				}
			}
			fmt.Println("login", fm.Login.UserName)
		case "ch", "channel":
			fm.GetCh()
			// fmt.Println(fm.Channels)
			for i, c := range fm.Channels.Channels {
				fmt.Printf("%v : %s(%s) ", c.ChannelId, c.Name, c.NameEn)
				if i%2 == 1 {
					fmt.Print("\n")
				}
			}
		case "pe", "people":

			cid := 0
			if len(inln) > 1 {
				cid, _ = strconv.Atoi(inln[1])
			} else {
				fmt.Print("ChannelId:")
				input, _, _ = reader.ReadLine()
				if string(input) != "" {
					cid, err = strconv.Atoi(string(input))
					if err != nil {
						fmt.Println(err.Error())
					}
				}
			}
			if cid != 0 {
				cidHave := true
				// for _, c := range fm.Channels.Channels {
				// 	switch c.ChannelId.(type) {
				// 	case string:
				// 		if v, _ := c.ChannelId.(string); v == string(cid) {
				// 			cidHave = true
				// 		}
				// 	case float64:
				// 		if v, _ := c.ChannelId.(float64); v == float64(cid) {
				// 			cidHave = true
				// 		}
				// 	}
				// }
				if cidHave {
					fmt.Printf("Get List of Channel:%v\n", cid)
					fm.GetPeople(int64(cid))
				}
			} else {
				fm.GetPeople(0)
			}
			// todo检测频道

			for i, s := range fm.PlayList {
				fmt.Printf("%d: %s - %s \n", i, s.Title, s.Artist)
			}

			// fmt.Println(fm.People)
		case "h", "help":
			fmt.Println(`h/help
bye: goodbye
login: login user password
ch/channel: get channellist,
pe/people: pe channel'id get song list of channel;
play: play songid`)
		case "fm":
			fmt.Println(fm)
		case "clear":
			fmt.Println("toclear!")
		case "play":
			i := 0
			if len(inln) > 1 {
				i, _ = strconv.Atoi(inln[1])
			} else {
				input, _, _ = reader.ReadLine()
				if string(input) != "" {
					i, err = strconv.Atoi(string(input))
					if err != nil {
						fmt.Println(err.Error())
					}
				}
			}
			go play(fm.PlayList[i].Url)
		default:
			fmt.Println("nothing... \nyou can input help or h!")
		}
		fmt.Print("> ")
	}

}

func play(url string) {
	// cmd := exec.Command("ffplay", url, "-nodisp", "-autoexit")
	// cmd := exec.Command("mplayer", url)
	// _, err := cmd.Output()
	// if err != nil {
	// 	log.Println(err.Error())
	// }
	// fmt.Fprintf(os.Stdout, "Result: %s", buf)

	sh.Command("mplayer", url).Run()
}
